package br.pucrs.lognplay.gateway;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

public final class SearchGateway {

	private static final String ELASTIC_SEARCH_HOST = "ec2-177-71-224-71.sa-east-1.compute.amazonaws.com";

	private static final Integer ELASTIC_SEARCH_PORT = 9300;

	private static final String LOGNPLAY_CLUSTER_NAME = "lognplayESCluster";

	private static SearchGateway instance;

	private Client client;

	private SearchGateway() {

	}

	public static SearchGateway getInstance() {
		if (instance == null) {
			instance = new SearchGateway();
		}

		return instance;
	}

	public void openClient() {
		Builder builer = ImmutableSettings.settingsBuilder();
		builer.put("cluster.name", LOGNPLAY_CLUSTER_NAME);
		builer.put("node.data", false);
		builer.build();

		// @formatter:off
		client = new TransportClient(builer)
			.addTransportAddress(new InetSocketTransportAddress(ELASTIC_SEARCH_HOST, ELASTIC_SEARCH_PORT));
		// @formatter:on
	}

	public Client getClient() {
		return client;
	}

	public void closeClient() {
		client.close();
	}
}
